<template>
  <el-dialog
    :title="title"
    v-model="dialogVisible"
    @close="dialogFormVisible"
    width="500px"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-form ref="formRef" :model="form" label-width="100px">
      <el-form-item label="当前状态">
        <el-tag :type="getStatusTag(info.status)">{{ info.status_text }}</el-tag>
      </el-form-item>
      <el-form-item label="新状态" prop="status">
        <el-select v-model="form.status" placeholder="请选择状态">
          <el-option label="待处理" value="pending"></el-option>
          <el-option label="审核中" value="reviewing"></el-option>
          <el-option label="进行中" value="in_progress"></el-option>
          <el-option label="已完成" value="completed"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="cancelFunc">取消</el-button>
      <el-button type="primary" @click="onSubmit" :loading="loading">确认</el-button>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { updateApi } from "@/api/temple";
import { ElMessage } from "element-plus";
import { inject, ref, watch } from "vue";
const title = ref("更新申请状态");
const dialogVisible = ref(false);
const info = ref<any>();
const form = ref({
  id: 0,
  status: "",
});
const formRef = ref<any>(null);
const loading = ref(false);
const dialogFormVisible = () => {
  loading.value = false;
  dialogVisible.value = false;
};
const open_type = ref("");
const open = (row?: any) => {
  info.value = row;
  form.value.id = row.id;
  form.value.status = row.status;
  dialogVisible.value = true;
};
const getStatusTag = (status: string) => {
  switch (status) {
    case "completed":
      return "success";
    case "reviewing":
      return "warning";
    case "pending":
      return "info";
    default:
      return "primary";
  }
};
const onSubmit = () => {
  formRef.value.validate((valid: any) => {
    if (valid) {
      loading.value = true;
      updateApi(form.value).then((res) => {
        loading.value = false;
        ElMessage.success("更新成功");
        dialogFormVisible();
      });
    } else {
      return false;
    }
  });
};
/*取消*/
const cancelFunc = () => {
  loading.value = false;
  dialogFormVisible();
};
defineExpose({ open });
</script>

<style lang="scss" scoped>
.tip {
  font-size: 14px;
  padding: 10px 0;
}
</style>
